package com.jbh.permissionlibrary;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import com.jbh.permissionlibrary.base.PermissionsResult;
import com.jbh.permissionlibrary.i.AgreePermissionListener;
import com.jbh.permissionlibrary.i.CheckPermissionListener;
import com.jbh.permissionlibrary.i.PermissionRequestCode;
import java.util.ArrayList;
import java.util.List;

public class PermissionManager {

    private static PermissionManager manager ;

    public static PermissionManager newInstance() {
        if (null==manager){
            synchronized (PermissionManager.class){
                if (null==manager){
                    manager = new PermissionManager();
                }
            }
        }
        return manager;
    }



    /**
     * 单个检测权限
     *
     * @param activity
     * @param permission
     * @param requestCode
     * @param listener
     */
    public void checkPermission(Activity activity,
                                 String permission,
                                 int requestCode,
                                 CheckPermissionListener listener) {
        final List<String> permissionsList = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if ((activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(permission);
            if (permissionsList.size() != 0) {
                activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), requestCode);
            }else {
                if (listener!=null)
                listener.onNoNeedCheckPermission();
            }
        }else {
            if (listener!=null)
            listener.onNoNeedCheckPermission();
        }
    }



    /**
     * 批量申请权限
     *
     * @param activity
     * @param list 需要申请的权限
     * @param listener
     */
    public void checkPermission(Activity activity,
                                List<String> list,
                                CheckPermissionListener listener)
    {

        List<String> permissionsList = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M&&list!=null&&list.size()>0) {
            for (String p:list){
                if ((activity.checkSelfPermission(p) != PackageManager.PERMISSION_GRANTED))
                    permissionsList.add(p);
            }
            if (permissionsList.size() != 0) {
                activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), PermissionRequestCode.REQUEST_CODE);
            }else {
                if (listener!=null)
                listener.onNoNeedCheckPermission();
            }
        }else {
            if (listener!=null)
            listener.onNoNeedCheckPermission();
        }
    }


    /**
     * 批量申请权限
     *
     * @param activity
     * @param list 需要申请的权限
     * @param listener
     */
    public void checkPermission(Activity activity,
                                List<String> list,
                                int requestCode,
                                CheckPermissionListener listener)
    {

        List<String> permissionsList = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M&&list!=null&&list.size()>0) {
            for (String p:list){
                if ((activity.checkSelfPermission(p) != PackageManager.PERMISSION_GRANTED))
                    permissionsList.add(p);
            }
            if (permissionsList.size() != 0) {
                activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        requestCode);
            }else {
                if (listener!=null)
                    listener.onNoNeedCheckPermission();
            }
        }else {
            if (listener!=null)
                listener.onNoNeedCheckPermission();
        }
    }



    /**
     * 设置回调
     *
     * @param activity
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    public void setPermissionsResult(Activity activity,
                                     int requestCode,
                                     String[] permissions,
                                     int[] grantResults, final AgreePermissionListener listener){
       new PermissionsResult(activity,requestCode,permissions,grantResults){
            @Override
            protected void onAgreePermission(int requestCode) {
                super.onAgreePermission(requestCode);
                if (listener!=null){
                    listener.onAgreePermission(requestCode);
                }
            }
        };
    }

}
